package com.tml.datastructure;

public class TreeRoot {

	public TreeNode treeNode = null;

	public void insert(int value) {
		TreeNode Node = new TreeNode(value);
		
		if (treeNode == null) {
			treeNode = Node;
		}
		else {
			TreeNode current = treeNode;
			TreeNode parent;
			while (true) {
				parent = current;
				if (value < current.data) {
					current = current.left;
					if (current == null) {
						parent.left = Node;
						return;
					}
					
				}else {
					current = current.rigth;
					if (current == null) {
						parent.rigth = Node;
						return;
					}
				}
				
			}
		}
	}
	/**
	 * 先序遍历
	 * @param treeNode
	 */
	public static void preTraverseBTree(TreeNode treeNode) {
		
		if (treeNode != null) {
			//根
			System.out.print(treeNode.getData()+"->");
			//左
			preTraverseBTree(treeNode.left);
			//右
			preTraverseBTree(treeNode.rigth);
		}
	}
	/**
	 * 中序遍历
	 * @param treeNode
	 */
	public static void inTraverseBTree(TreeNode treeNode) {
		if (treeNode != null) {
			//左
			inTraverseBTree(treeNode.left);
			//根
			System.out.print(treeNode.getData()+"->");
			//右
			inTraverseBTree(treeNode.rigth);
		}
	}
	
	public static void main(String[] args) {
		int[] arrays = {8,3,2,1,9,4,7,6,5};
		int[] arrays1 = {1,2,3,4,5,6,7,8,9};
		TreeRoot treeRoot = new TreeRoot();
		for (int i : arrays) {
			treeRoot.insert(i);
		}
		preTraverseBTree(treeRoot.treeNode);
		System.out.println();
		inTraverseBTree(treeRoot.treeNode);
	}
}
